namespace QMVnSoft.SystemForms
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;
    using QMVnSoft.DAL.Util;    

    public class FormSerialKey : FormControl
    {
        private BtnControl btnClose;
        private BtnControl btnOk;
        private BtnControl btnPathBackup;
        private IContainer components;
        private LabelControl lblPathBackup;
        private TextControl txtRegisteredFile;

        public FormSerialKey()
        {
            this.InitializeComponent();
            this.Text = AppInstance.SoftwareName;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Register();
            base.Close();
        }

        private void btnPathBackup_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = "";
            dialog.Filter = "Registered file (*.dll)|*.dll";
            dialog.CheckFileExists = true;
            dialog.ShowDialog();
            this.txtRegisteredFile.Text = dialog.FileName;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormSerialKey));
            this.btnOk = new QMVnSoft.Controls.BtnControl();
            this.btnClose = new QMVnSoft.Controls.BtnControl();
            this.btnPathBackup = new QMVnSoft.Controls.BtnControl();
            this.lblPathBackup = new QMVnSoft.Controls.LabelControl();
            this.txtRegisteredFile = new QMVnSoft.Controls.TextControl();
            this.SuspendLayout();
            // 
            // btnOk
            // 
            this.btnOk.Font = new System.Drawing.Font("Arial", 8.25F);
            this.btnOk.Location = new System.Drawing.Point(33, 104);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new System.Drawing.Size(100, 23);
            this.btnOk.TabIndex = 2;
            this.btnOk.Text = "&Chấp nhận";
            this.btnOk.UseVisualStyleBackColor = true;
            this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
            // 
            // btnClose
            // 
            this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnClose.Font = new System.Drawing.Font("Arial", 8.25F);
            this.btnClose.Location = new System.Drawing.Point(166, 104);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(100, 23);
            this.btnClose.TabIndex = 3;
            this.btnClose.Text = "&Quay ra";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // btnPathBackup
            // 
            this.btnPathBackup.Font = new System.Drawing.Font("Arial", 8.25F);
            this.btnPathBackup.Location = new System.Drawing.Point(255, 25);
            this.btnPathBackup.Name = "btnPathBackup";
            this.btnPathBackup.Size = new System.Drawing.Size(29, 23);
            this.btnPathBackup.TabIndex = 1;
            this.btnPathBackup.TabStop = false;
            this.btnPathBackup.Text = "...";
            this.btnPathBackup.UseVisualStyleBackColor = true;
            this.btnPathBackup.Click += new System.EventHandler(this.btnPathBackup_Click);
            // 
            // lblPathBackup
            // 
            this.lblPathBackup.AutoSize = true;
            this.lblPathBackup.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lblPathBackup.Location = new System.Drawing.Point(9, 29);
            this.lblPathBackup.Name = "lblPathBackup";
            this.lblPathBackup.Size = new System.Drawing.Size(84, 14);
            this.lblPathBackup.TabIndex = 6;
            this.lblPathBackup.Text = "Tên tệp đăng ký";
            // 
            // txtRegisteredFile
            // 
            this.txtRegisteredFile.CheckTextSearched = false;
            this.txtRegisteredFile.DisabledForeColor = System.Drawing.Color.Blue;
            this.txtRegisteredFile.Font = new System.Drawing.Font("Arial", 8.25F);
            this.txtRegisteredFile.Location = new System.Drawing.Point(12, 51);
            this.txtRegisteredFile.Name = "txtRegisteredFile";
            this.txtRegisteredFile.OldValue = null;
            this.txtRegisteredFile.Size = new System.Drawing.Size(272, 20);
            this.txtRegisteredFile.TabIndex = 0;
            this.txtRegisteredFile.UserTextLength  = QMVnSoft.AppInstance.TextControlLength.None;
            this.txtRegisteredFile.UserTrimMode  = QMVnSoft.AppInstance.TextControlTrimMode.Right;
            this.txtRegisteredFile.Value = ((object)(resources.GetObject("txtRegisteredFile.Value")));
            // 
            // FormSerialKey
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.CancelButton = this.btnClose;
            this.ClientSize = new System.Drawing.Size(299, 139);
            this.Controls.Add(this.btnPathBackup);
            this.Controls.Add(this.lblPathBackup);
            this.Controls.Add(this.txtRegisteredFile);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnOk);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormSerialKey";
            this.Text = "Phần mềm kế toán doanh nghiệp";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private bool Register()
        {            
            try
            {
                if (AppDataAccess.Register(txtRegisteredFile.Text.Trim()))
                    AppInstance.CompanyRegister();
                MessageBox.Show("Đã đăng ký thành công!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}

